//
//  ECUIVC_ToComment.m
//  Edu_iOS2018
//
//  Created by yanyan on 2018/1/18.
//  Copyright © 2018年 yanyan. All rights reserved.
//

#import "ECUIVC_ToComment.h"
#import "ECUITVCell_CourseOrder.h"
#import "CYUIView_StarLevel.h"
#import "ECCourseOrderModel.h"
#import "UITextView+Placeholder.h"
#import "UITextView+KTLimitCounter.h"
@interface ECUIVC_ToComment ()
@property(nonatomic,strong)ECUIView_CourseOrder *courseView;
@property(nonatomic,strong)CYUIView_StarLevel *starView;
@property(nonatomic,strong)UILabel *starLevelLabel;
@property(nonatomic,strong)UITextView *textView;
@property(nonatomic,strong)UIButton *commentBtn;
@end

@implementation ECUIVC_ToComment

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"课程评价";
    [self.view addSubview:self.courseView];
    [self.view addSubview:self.starView];
    [self.view addSubview:self.starLevelLabel];
    [self.view addSubview:self.textView];
    [self.view addSubview:self.commentBtn];
}
- (UILabel *)starLevelLabel
{
    if (!_starLevelLabel) {
        _starLevelLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth - 90, self.starView.centerY - 20, 80, 40)];
        _starLevelLabel.font = [UIFont systemFontOfSize:16];
        _starLevelLabel.textColor = [UIColor colorWithHex:0xff9900];
        _starLevelLabel.text = [NSString stringWithFormat:@"%d星好评",(int)self.starView.defaultScore];
        _starLevelLabel.textAlignment = NSTextAlignmentRight;
    }
    return _starLevelLabel;
}
-(void)setObject:(id)object
{
    [super setObject:object];
    ECCourseOrderModel *mo = object;

    [self.courseView.courseImageView sd_setImageWithURL:[NSURL URLWithString:mo.mo_course.pictureUrl] placeholderImage:nil];
    self.courseView.courseNameLabel.text = mo.mo_course.title;
    self.courseView.courseTeacherLabel.text = [NSString stringWithFormat:@"任课老师: %@",mo.mo_course.teacher];
    self.courseView.priceLabel.attributedText = [ECFormatHelper attributedStringForPromotionPrice:mo.mo_course.price decimal:2 smallFontSize:10];
}
-(void)requestToComment{
//    if (self.textView.text.length < 5) {
//        [MBProgressHUD showTipMessageInWindow:@"评价字数至少5个字"];
//        return;
//    }
    [self.view endEditing:YES];

    ECCourseOrderModel *mo = self.object;
    [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];

    [HWHttpTool requestWithMethod:@"POST" url:URL_ToCourseCommentWithId(mo.mo_course.modelId) params:@{@"rating":@(self.starView.score),@"content":self.textView.text} headers:@{@"X-AUTH-TOKEN":[UserInfoManager  shareUser].token} success:^(id json) {
        [MBProgressHUD hideHUD];
        if ([[json valueForKey:@"code"] integerValue] == 200) {
            // 评价完成
            [MBProgressHUD showTipMessageInWindow:@"提交成功"];
            // 返回课程列表页，刷新
            WeakSelf(weakSelf);
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf commentSuccess];
            });
        } else {
            [MBProgressHUD showTipMessageInWindow:@"该课程评价失败"];

        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showTipMessageInWindow: error.description];

    }];
}
-(void)commentSuccess
{
//    [self.navigationController popViewControllerAnimated:YES];
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
}
- (ECUIView_CourseOrder *)courseView
{
    if (!_courseView) {
        _courseView = [[ECUIView_CourseOrder alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kProportion_Height(246))];
        
    }
    return _courseView;
}
- (UITextView *)textView
{
    if (!_textView) {
        _textView = [[UITextView alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(self.starView.frame), kScreenWidth-20, 100)];
        _textView.placeholder = @" 写下您对课程和讲师的感受吧";
        _textView.placeholderColor = [UIColor colorWithHex:0xcccccc];
        _textView.placeholderLabel.font = [UIFont systemFontOfSize:15];
        _textView.kt_labMargin = 15;
        _textView.kt_labHeight = 15;
        _textView.kt_inputLimitLabel.textColor = [UIColor colorWithHex:0xcccccc];
        _textView.kt_limitCount = 100;
        _textView.font = [UIFont systemFontOfSize:15];
        _textView.layer.borderWidth = 1;
        _textView.layer.masksToBounds = YES;
        _textView.autocorrectionType = UITextAutocorrectionTypeNo;
        _textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
        _textView.layer.borderColor = [[UIColor colorWithHex:0xe5e5e5] CGColor];
        _textView.textColor = [UIColor colorWithHex:0x454545];
    }
    return _textView;
    
}
- (UIButton *)commentBtn
{
    if (!_commentBtn) {
        _commentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_commentBtn setBackgroundImage:[UIImage imageNamed:@"flatButton_colorNormal"] forState:UIControlStateNormal];
        _commentBtn.frame = CGRectMake(kProportion_Width(60), CGRectGetMaxY(self.textView.frame) + 10, kScreenWidth - kProportion_Width(60*2),(kScreenWidth - kProportion_Width(60*2))* 0.1406);
        [_commentBtn addTarget:self action:@selector(action_comment:) forControlEvents:UIControlEventTouchUpInside];
        [_commentBtn setBackgroundImage:[UIImage imageNamed:@"flatButton_colorSelected"] forState:UIControlStateHighlighted];
        [_commentBtn setTitle:@"提交" forState:UIControlStateNormal];
        _commentBtn.titleLabel.font = [UIFont systemFontOfSize:ceil(20)];
        [_commentBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_commentBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    }
    return _commentBtn;
}
-(void)action_comment:(id)sender
{
    [self requestToComment];
}
- (CYUIView_StarLevel *)starView
{
    if (!_starView) {
        _starView = [[CYUIView_StarLevel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(self.courseView.frame), kScreenWidth-110, 100)];
        _starView.hPadding = 10;
        _starView.imageWidth = 35;
        _starView.minScore = 1;
        _starView.normalImageName = @"cc_grayStar";
        _starView.selectedImageName = @"cc_highlightStar";
        _starView.defaultScore = 5;
        
    }
    WeakSelf(weakSelf);
    _starView.block_score = ^(id obj) {
        weakSelf.starLevelLabel.text = [NSString stringWithFormat:@"%ld星好评",(long)[obj integerValue]];
    };
    return _starView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
